package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.apache.commons.io.IOUtils;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.ws.rs.Path;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/deploy")
public class Camunda01Deploy {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 根据classpath中的文件部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/deploy/deployByClasspath
    @RequestMapping(value="/deployByClasspath",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deployByClasspath(){
        String deployId = this.workFlowService.deploy("processes/01deployTest.bpmn");
        System.out.println("deployId:"+deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 根据bpmn的字符串内容部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/deploy/deployByString
    @RequestMapping(value="/deployByString",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deployByString() throws Exception{

        InputStream is = this.getClass().getResourceAsStream("/processes/01deployTest.bpmn");
System.out.println(is);
//        从文件中读取
//        InputStream is = new FileInputStream(new File("D:\\devil13th\\gitee\\com-thd-springboottest-parent\\com-thd-springboottest-camunda\\src\\main\\resources\\processes\\01normal.bpmn"));

        String bpmnContext = IOUtils.toString(is);
        System.out.println(bpmnContext);
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addString("01deployTest.bpmn",bpmnContext)
                .deploy();
        System.out.println(deployment.getId());
        System.out.println(deployment.getTenantId());
        return ResponseEntity.ok("SUCCESS");
    }




    /**
     * 根据classpath中的文件部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/deploy/deployByTenantId
    @RequestMapping(value="/deployByTenantId",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deployByTenantId(){
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .tenantId("thd")//指定租户
                .addClasspathResource("processes/01deployTest.bpmn")//指定部署的流程
                .deploy();
        System.out.println(deployment.getId());
        System.out.println(deployment.getTenantId());
        return ResponseEntity.ok("SUCCESS");
    }


    /**
     * 根据流程部署ID删除部署及流程定义和实例
     */
    // url : http://127.0.0.1:8899/thd/camunda/deploy/deleteDeploy/{id}
    @RequestMapping(value="/deleteDeploy/{deployId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deleteDeploy(@PathVariable String deployId){
        this.workFlowService.deleteDeployCascade(deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 根据流程定义ID删除流程定义及实例
     */
    // url : http://127.0.0.1:8899/thd/camunda/deploy/deleteProcDef/{id}
    @RequestMapping(value="/deleteProcDef/{procDefId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deleteProcDef(@PathVariable String procDefId){
        this.workFlowService.deleteProcessDefinitionCascade(procDefId);
        return ResponseEntity.ok("SUCCESS");
    }





}
